<template>
    <div :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import _ from 'lodash'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'
export default {
    mixins: [resize],
    name: "LineChart",
    //父子组件通信
    props: {
        //长宽属性
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
    },
    data() {
        return {
            //echart对象
            chart: null,
            options:{
                //echart标题
                // title:{
                //     text:'',
                // },
                //调整左右上下边距
                grid:{
                    left: 10,
                    right: 20,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                //图例
                legend:{
                    data: ['预期', '实际']
                },
                //
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                //x轴
                xAxis: {
                    data: [],
                    boundaryGap: false,
                    axisTick: {
                        show: false
                    }
                },
                //y轴
                yAxis: {
                    axisTick: {
                        show: false
                    }
                },
                //填充数据及展示效果
                series:[
                    {
                        name: '预期',
                        itemStyle: {
                            normal: {
                                color: '#FF005A',
                                lineStyle: {
                                    color: '#FF005A',
                                    width: 2
                                }
                            }
                        },
                        smooth: true,
                        type: 'line',
                        data: [],
                        animationDuration: 2800,
                        animationEasing: 'cubicInOut'
                    },
                    {
                        name: '实际',
                        smooth: true,
                        type: 'line',
                        itemStyle: {
                            normal: {
                                color: '#3888fa',
                                lineStyle: {
                                    color: '#3888fa',
                                    width: 2
                                },
                                areaStyle: {
                                    color: '#f3f8ff'
                                }
                            }
                        },
                        data: [],
                        animationDuration: 2800,
                        animationEasing: 'quadraticOut'
                    }
                ],
            }
        }
    },
    methods: {
        //初始化echart
        initChart() {
            this.chart = this.$echarts.init(this.$el, 'macarons')
            this.setOptions()
        },
        //echart赋予属性和值
        async setOptions(){
            const data ={
                "xAxis": {
                    "data": [
                        "星期1",
                        "星期2",
                        "星期3",
                        "星期4",
                        "星期5",
                        "星期6",
                        "星期7"
                    ]
                },
                "series":[
                    {
                        "name": "预期",
                        "data": [100, 120, 161, 134, 105, 160, 165]

                    },
                    {
                        "name": "实际",
                        "data": [120, 82, 91, 154, 162, 140, 145]
                    }
                ]
            }
            const result = _.merge(this.options,data)
            // const result = {...this.options,...data}
            // const result = Object.assign(this.options,data)
            console.log(result)
            this.chart.setOption(result)
            }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    watch: {},

    //echart对象销毁
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    }
}

</script>

<style lang="less" scoped>

</style>
